package com.celesea.workflow.core.service;

import com.celesea.framework.service.AbstractBaseService;
import com.celesea.framework.util.CollectionKit;
import com.celesea.framework.util.EnvUtil;
import com.celesea.framework.util.StringKit;
import com.celesea.workflow.core.dao.WorkflowPermissionDao;
import com.celesea.workflow.core.entity.WorkflowPermission;
import com.celesea.workflow.core.entity.WorkflowPermission.WorkflowPermissionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author JackYuan
 * @create 2017-11-28 12:53
 */
@Service
public class WorkflowPermissionServiceImpl
        extends AbstractBaseService<WorkflowPermission> implements WorkflowPermissionService {
    @Autowired
    private WorkflowPermissionDao workflowPermissionDao;

    @Override
    public boolean hasStatisticsPerm(Integer workflowType) {
        List<WorkflowPermission> perms = this.getWorkflowPermission(
                workflowType, WorkflowPermissionType.STATISTICS.getCode());
        if (CollectionKit.isEmpty(perms)) {
            return false;
        }
        for (WorkflowPermission perm : perms) {
            if (isPermUser(perm) || isPermRole(perm)) {
                return true;
            }
        }
        return false;
    }


    private boolean isPermRole(WorkflowPermission perm) {
        if (StringKit.isNotEmpty(perm.getRoleId())) {
            return EnvUtil.hasAnyRoleIds(perm.getRoleId());
        }
        return false;
    }

    private boolean isPermUser(WorkflowPermission perm) {
        String userId = EnvUtil.getLoginUser().getId();
        return perm.getUserId() != null && userId.equals(perm.getUserId());
    }

    private boolean isPermDept(WorkflowPermission perm) {
        String detpId = EnvUtil.getLoginDept().getId();
        return perm.getDeptId() != null && detpId.equals(perm.getDeptId());
    }


    private boolean isPermGroup(WorkflowPermission perm) {
        return EnvUtil.hasAnyGroups(perm.getGroupId());
    }

    private List<WorkflowPermission> getWorkflowPermission(Integer workflowType, Integer permissionType) {
        Example example = new Example(WorkflowPermission.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("tenantId", EnvUtil.getTenantId())
                .andEqualTo("workflowType", workflowType)
                .andEqualTo("type", permissionType);
        List<WorkflowPermission> perms = workflowPermissionDao.selectByExample(example);
        return perms;
    }


    /**
     * 是否有提交授权
     *
     * @param workflowType
     * @return
     */
    @Override
    public boolean hasSubmitPerm(Integer workflowType) {
        List<WorkflowPermission> perms = this.getWorkflowPermission(
                workflowType, WorkflowPermissionType.SUBMIT.getCode());
        if (CollectionKit.isEmpty(perms)) {
            return false;
        }
        for (WorkflowPermission perm : perms) {
            if (isPermUser(perm) || isPermRole(perm) || isPermDept(perm) || isPermGroup(perm)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 是否有提交授权
     *
     * @param workflowType
     * @return
     */
    @Override
    public boolean hasDefinePerm(Integer workflowType) {
        List<WorkflowPermission> perms = this.getWorkflowPermission(
                workflowType, WorkflowPermissionType.DEFINE.getCode());
        if (CollectionKit.isEmpty(perms)) {
            return false;
        }
        for (WorkflowPermission perm : perms) {
            if (isPermUser(perm) || isPermRole(perm) || isPermDept(perm) || isPermGroup(perm)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public List<WorkflowPermission> selectByWorkflow(Integer workflowType) {
        Example example = new Example(WorkflowPermission.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("tenantId", EnvUtil.getTenantId())
                .andEqualTo("workflowType", workflowType);
        List<WorkflowPermission> perms = workflowPermissionDao.selectByExample(example);
        return perms;
    }

    /**
     * 保存流程权限
     *
     * @param workflowType
     * @param workflowPermissions
     */
    @Override
    public void savePermission(Integer workflowType, List<WorkflowPermission> workflowPermissions) {
        this.clearWorkflowPermission(workflowType);
        super.insertList(workflowPermissions);
    }

    private int clearWorkflowPermission(Integer workflowType) {
        Example example = new Example(WorkflowPermission.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("tenantId", EnvUtil.getTenantId())
                .andEqualTo("workflowType", workflowType);
        return workflowPermissionDao.deleteByExample(example);
    }


}
